package flatlandAgent;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.List;

import evolutionaryLoop.EvolutionaryLoop;
import evolutionaryLoop.Population;
import evolutionaryLoop.selectionMechanisme.SigmaSelection;
import evolutionaryLoop.selectionProtocol.OverProduction;
import evolutionaryProblems.Phenotype;
import evolutionaryProblems.Problem;
import genotype.FlatlandGenotype;
import gui.GUIFrame;
import gui.LineChart;

public class FlatlandProblem extends Problem<FlatlandParameters> {
	
//	private FlatlandParameters parameters;
	private FlatlandProblem problem;
//	private EvolutionaryLoop evLoop;
//	private GUIFrame guiFrame; 
	private FlatlandSettingPanel settingPanel; 
//	private LineChart lineChart;
//	private GameModel gameModel; 
	
	public static void main(String[] args) {
		new FlatlandProblem();
//		problem.initialise();
//		problem.gui.setVisible(true);
	}
	
	public FlatlandProblem(){
//		super(parameters)
		problem = this; 
		this.initialise();
		
	}
	
	protected void initGUI(){
		guiFrame = new GUIFrame(this); 
		
		settingPanel = new FlatlandSettingPanel(problem, parameters); 
		settingPanel.showDefaultParameters(); 
		guiFrame.addSettingPanel(settingPanel); 
		
		lineChart = new LineChart();
		guiFrame.addLineChart(lineChart); 
		
		gameModel = new FlatlandGameModel(guiFrame,parameters); 
//		GUIFrame extraframe = new GUIFrame(); 
		guiFrame.addGamePanel(gameModel.getGamePanel());
//		extraframe.pack(); 
		
		guiFrame.pack(); 
		guiFrame.repaint(); 
		}
	
	protected void initParameters(){
		parameters = new FlatlandParameters();
		int[] nofNeuronsPerLayer = {6,3};
		
		parameters.setMaxNumberOfIterations(1000); 
		parameters.setMutationRate(0.05); 
		parameters.setCrossoverRate(0.9); 
		parameters.setNumOfChildenToBeGenerated(100); 
		parameters.setPopulationSize(100); 
		parameters.setElitims(1);
		parameters.setInterationsPerMap(50);
		parameters.setFoodDist(0.5);
		parameters.setPoisonDist(0.5);
		parameters.setNofMaps(5);
		parameters.setNetworkLayout(nofNeuronsPerLayer);
		parameters.setCulling(0);
		parameters.setCrossoverPoints(1); 
		parameters.setAdultSelectionProtocol(new OverProduction(parameters)); 
		parameters.setParenetSelectionMechanism(new SigmaSelection()); 
		parameters.setFitnessFunction(new FlatlandFitnessFunction(parameters));
		parameters.setDynamicMap(false); 
		
		FileSystem fileSys = FileSystems.getDefault();
		String sep = fileSys.getSeparator();
		parameters.setWriteStatistics("FlatlandProblem" + sep + "Project3" + sep + "Flatland ");
		
	}

//	@Override
//	public void startSimulation() {
//		emptyLineChart();
//		
//		evLoop = new EvolutionaryLoop(parameters, new Population(flatlandList), problem); 
//		evLoop.runLoop();
//	}

	@Override
	protected Population generatePopulation() {
		List<Phenotype> flatlandList = new ArrayList<Phenotype>(parameters.getPopulationSize()); 
		for (int i = 0; i < parameters.getPopulationSize(); i++) {
			flatlandList.add(new FlatlandGenotype(parameters.getNetworkLayout(), parameters).evolveGenotype()); 
		}
		return new Population(flatlandList);
	}


}
